//
//  MusicFormatDelegate.m
//  Sofia
//
//  Created by Anton Chebotaev on 23/11/08.
//  Copyright 2008 IFMO. All rights reserved.
//

#import "SFMusicFormatDelegate.h"
#import "SFMusicFormatToken.h"


@implementation SFMusicFormatDelegate

#pragma mark delegate methods
- (id)tokenField:(NSTokenField *)tokenField representedObjectForEditingString:(NSString *)editingString
{
	return [[SFMusicFormatToken alloc] initWithString:editingString];
}
- (NSString *)tokenField:(NSTokenField *)tokenField displayStringForRepresentedObject:(id)representedObject
{
	return [representedObject presentation];
}
- (NSString *)tokenField:(NSTokenField *)tokenField editingStringForRepresentedObject:(id)representedObject
{
	return [representedObject content];
}
- (NSTokenStyle)tokenField:(NSTokenField *)tokenField styleForRepresentedObject:(id)representedObject
{
	return [representedObject getStyle];
}
- (NSArray *)tokenField:(NSTokenField *)tokenField completionsForSubstring:(NSString *)substring 
		   indexOfToken:(NSInteger)tokenIndex 
	indexOfSelectedItem:(NSInteger *)selectedIndex
{
	return [NSArray array];
}
- (BOOL)tokenField:(NSTokenField *)tokenField writeRepresentedObjects:(NSArray *)objects toPasteboard:(NSPasteboard *)pboard
{
	// Declare types
    [pboard declareTypes:[NSArray arrayWithObject:[SFMusicFormatToken description]]
				   owner:self];
	
    // Copy data to the pasteboard
    [pboard setData:[NSKeyedArchiver archivedDataWithRootObject:objects] forType:[SFMusicFormatToken description]];
	
	//NSLog(@"writing %@ to dragPasteboard", objects);
	return YES;
}
- (NSArray *)tokenField:(NSTokenField *)tokenField readFromPasteboard:(NSPasteboard *)pboard
{
	// Is there a string on the pasteboard?
    NSArray *types = [pboard types];
	if ([types containsObject:[SFMusicFormatToken description]]) {
		
        // Read the data from the pasteboard
		NSData *data = [pboard dataForType:[SFMusicFormatToken description]];
        NSArray *result = [NSKeyedUnarchiver unarchiveObjectWithData:data];
		
		NSLog(@"readingFromPasteboard returns %@", result);
		
		return result;
    }
	else if ([types containsObject:NSStringPboardType]) {
		
        // Read the string from the pasteboard
		NSString *string = [pboard stringForType:NSStringPboardType];
        NSArray *result = [NSArray arrayWithObject:[SFMusicFormatToken tokenWithString:string]];
		
		NSLog(@"readingFromPasteboard returns %@", result);
		
		return result;
    }	
    return NO;
	
}

@end
